package priv.lirenhe.exam02.A;

import java.util.LinkedList;

public class Example04 implements Runnable{

	private LinkedList<String> linkedList;
	public volatile boolean exit = false; 

	
	public Example04(LinkedList<String> linkedList){
		this.linkedList = linkedList;
	}
	
	public static void main(String[] args) {
		//给定一个LinkedList集合linkedList，向里面添加10个元素
		LinkedList<String> linkedList = new LinkedList<String>();
		for(int i=0; i<10; i++){
			linkedList.add( i + "" );
		}
		Example04 exam = new Example04(linkedList);
		
		Thread thread01 = new Thread(exam);
		thread01.setName("thread01");
		Thread thread02 = new Thread(exam);
		thread02.setName("thread02");
		
		thread01.start();
		thread02.start();
	}

	@Override
	public void run() {
		while(!exit){
//			System.out.println(Thread.currentThread().getName());
			String str = readOneObj();
			if(str == null){
				this.exit = true;
			}else{
				System.out.println(Thread.currentThread().getName() + "--->" + str);
			}
		}
	}
	
	public synchronized String readOneObj(){
		if(linkedList.size() <=0 ){
			return null;
		}
		String str = this.linkedList.get(0);
		if(str != null){
			this.linkedList.remove(str);
		}
		return str;
	}

}
